﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using System.Web;
using System.Web.UI;
using PetShop.Web.Entities;
using PetShop.Web.WebComponents;

namespace PetShop.Web
{
    public partial class WebFormEditAccount : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var myAccount = (AccountInfo) HttpContext.Current.Session["ACCOUNT_KEY"];

                if (myAccount == null)
                {
                    HttpContext.Current.Response.Redirect("WebFormSignIn.aspx", true);
                }
                else
                {
                    lblUserId.Text = myAccount.UserId;
                    txtEmail.Text = myAccount.Email;
                    addr.Address = myAccount.Address;
                    prefs.Language = myAccount.Language;
                    prefs.Category = myAccount.Category;
                    prefs.IsShowBanners = myAccount.IsShowBanners;
                    prefs.IsShowFavorites = myAccount.IsShowFavorites;
                }
            }
        }


        private void InitializeComponent()
        {
        }

        protected void SubmitClicked(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                var myAccount = (AccountInfo) HttpContext.Current.Session["ACCOUNT_KEY"];

                if (myAccount == null)
                {
                    HttpContext.Current.Response.Redirect("WebFormSignIn.aspx", true);
                }
                else
                {
                    string email = CleanString.InputText(txtEmail.Text, 50);
                    AddressInfo address = addr.Address;
                    string language = prefs.Language;
                    string favCategory = prefs.Category;
                    bool showFavorites = prefs.IsShowFavorites;
                    bool showBanners = prefs.IsShowBanners;

                    var updatedAccountInfo = new AccountInfo(myAccount.UserId, myAccount.Password, email, address,
                                                             language, favCategory, showFavorites, showBanners);


                    string sqlUpdateAccount = "UPDATE Account " +
                                              " SET Email = @Email, FirstName = @FirstName, LastName = @LastName, " +
                                              " Addr1 = @Address1, Addr2 = @Address2, City = @City, State = @State, " +
                                              " Zip = @Zip, Country = @Country, Phone = @Phone ,LangPref = @Language, FavCategory = @Category, MyListOpt = @ShowFavorites, BannerOpt = @ShowBanners " +
                                              " WHERE UserId = @UserId";


                    using (var ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        using (var cmd = new SqlCommand())
                        {
                            cmd.CommandText = sqlUpdateAccount;
                            cmd.CommandType = CommandType.Text;
                            cmd.Parameters.AddWithValue("@Email", updatedAccountInfo.Email);
                            cmd.Parameters.AddWithValue("@FirstName", updatedAccountInfo.Address.FirstName);
                            cmd.Parameters.AddWithValue("@LastName", updatedAccountInfo.Address.LastName);
                            cmd.Parameters.AddWithValue("@Address1", updatedAccountInfo.Address.Address1);
                            cmd.Parameters.AddWithValue("@Address2", updatedAccountInfo.Address.Address2);
                            cmd.Parameters.AddWithValue("@City", updatedAccountInfo.Address.City);
                            cmd.Parameters.AddWithValue("@State", updatedAccountInfo.Address.State);
                            cmd.Parameters.AddWithValue("@Zip", updatedAccountInfo.Address.Zip);
                            cmd.Parameters.AddWithValue("@Country", updatedAccountInfo.Address.Country);
                            cmd.Parameters.AddWithValue("@Phone", updatedAccountInfo.Address.Phone);
                            cmd.Parameters.AddWithValue("@UserId", updatedAccountInfo.UserId);
                            cmd.Parameters.AddWithValue("@Language", updatedAccountInfo.Language);
                            cmd.Parameters.AddWithValue("@Category", updatedAccountInfo.Category);
                            cmd.Parameters.AddWithValue("@ShowFavorites", updatedAccountInfo.IsShowFavorites);
                            cmd.Parameters.AddWithValue("@ShowBanners", updatedAccountInfo.IsShowBanners);
                            using (
                                var conn =
                                    new SqlConnection(ConfigurationManager.AppSettings["SQLConnString"]))
                            {
                                cmd.Connection = conn;

                                conn.Open();
                                cmd.ExecuteNonQuery();
                            }
                        }

                        ts.Complete();
                    }


                    HttpContext.Current.Session["ACCOUNT_KEY"] = updatedAccountInfo;


                    HttpContext.Current.Response.Redirect("MyAccount.aspx?action=update", true);
                }
            }
        }
    }
}